const { JJCourse, JJCourseCamp, JJCourseImages, JJUser, JJUserCourse, Ctcdb, JJUserCourseSettle, JJCourseTicket, City }
= require('../../models/ctcdb');
const moment = require('moment');
const _ = require('lodash');
const BaseResp = require('../../utils/baseResp');
const errorCode = require('../../utils/errorCode');
const courseCommonService = require('./commonService');
const tools = require('../../utils/tools');
const Logger = require('../../utils/logger').Logger('course');

/**
 * 添加课程
 * @param userId 用户Id
 * @param type 类别
 * @param bigCourse 团课信息
 * @param selfCourse 私教课信息
 * @param camp 训练营信息
 * @param addressInfo 地址信息
 * @returns {Promise<void>}
 */
async function add({ userId, type, bigCourse, selfCourse, camp }) {
  type = parseInt(type);
  const course = {
    status: 1, // 默认是通过
    cStatus: 1  // 默认是可以预约的
  };
  let images = [];
  let details;
  const user = await JJUser.findOne({
    where: {
      id: userId
    },
    attributes: ['identity', 'isForbidden'],
    raw: true
  });
  if (!user) {
    throw BaseResp.error(errorCode.USER.NOT_FOUND)
  }
  if (user.identity !== 1) {
    throw BaseResp.error(errorCode.USER.NOT_JIAO_LIAN)
  }
  // 暂时终止合作， 不能发课
  if (user.isForbidden) {
    throw BaseResp.error(errorCode.USER.HAS_END_SIGN)
  }
  if (type === 1) {
    if (new Date(bigCourse.startTime) < Date.now()) {
      throw BaseResp.error(errorCode.COURSE.START_TIME_LESS)
    }
    if (new Date(bigCourse.startTime) < new Date(bigCourse.signEndTime).getTime()) {
      throw BaseResp.error(errorCode.COURSE.SIGN_TIME_IS_BIG)
    }
    Object.assign(course, {
      courseName: bigCourse.courseName,
      totalMoney: bigCourse.totalMoney,
      storeName: bigCourse.storeName,
      description: bigCourse.description,
      startTime: bigCourse.startTime,
      classNum: bigCourse.classNum,
      peopleNum: bigCourse.peopleNum,
      classTime: bigCourse.classTime,
      signEndTime: bigCourse.signEndTime,
      label: bigCourse.label,
      maxNum: bigCourse.maxNum || 99,
      JUserId: userId,
      type
    }, bigCourse.addressInfo || {});
    images = bigCourse.images
  }
  if (type === 2) {
    if (new Date(selfCourse.signEndTime).getTime() < Date.now()) {
      throw BaseResp.error(errorCode.COURSE.START_TIME_LESS)
    }
    Object.assign(course, {
      courseName: selfCourse.courseName,
      totalMoney: selfCourse.totalMoney,
      storeName: selfCourse.storeName,
      description: selfCourse.description,
      // signEndTime: selfCourse.signEndTime,
      label: selfCourse.label,
      JUserId: userId,
      isSend: selfCourse.isSend || 0, // 是否推送
      type
    }, selfCourse.addressInfo || {});
    images = selfCourse.images
  }
  if (type === 3) {
    if (new Date(camp.signEndTime).getTime() < Date.now()) {
      throw BaseResp.error(errorCode.COURSE.START_TIME_LESS)
    }
    Object.assign(course, {
      courseName: camp.courseName,
      totalMoney: camp.totalMoney,
      peopleNum: camp.peopleNum,
      description: camp.description,
      signEndTime: camp.signEndTime,
      startTime: camp.signEndTime, // 训练营针对课程
      label: camp.label,
      maxNum: camp.maxNum || 99,
      JUserId: userId,
      type
    }, camp.addressInfo || {});
    images = camp.images;
    details = camp.details;
  }
  // todo 加上事务
  const transaction = await Ctcdb.transaction();
  try {
    const { id } = await JJCourse.create(course, { transaction });
    const imageArr = [];
    images.forEach((item) => {
      imageArr.push({
        JUserId: userId,
        courseId: id,
        imagePath: item
      });
    });
    await JJCourseImages.bulkCreate(imageArr, { transaction });
    if (details) {
      const arr = [];
      details.forEach((item) => {
        arr.push({
          className: item.className,
          storeName: item.storeName,
          startTime: item.classStartTime,
          courseId: id,
          JUserId: userId,
          cStatus: 1
        })
      });
      await JJCourseCamp.bulkCreate(arr, { transaction }); // 创建课时
      // const userDetailArr = [];
      // campArr.forEach((item) => {
      //   userDetailArr.push({
      //     courseId: id,
      //     status: 1,
      //     jUserId: userId,
      //     : userId,
      //     type: 2,
      //     campId: item.id
      //   });
      // });
      // await JJUserCourse.bulkCreate(userDetailArr)
    }
    await transaction.commit();
    // await JJUserCourse.create({
    //   jUserId: userId,
    //   status: 1,
    //   courseId: id,
    //   lUserId: userId, // 教练就是自己
    //   type: 1,
    //   campId: 0
    // })
    return id
  } catch (e) {
    Logger.error(e);
    if (transaction) {
      await transaction.rollback();
    }
    return Promise.reject(e)
  }
}

/**
 * 查询课程列表
 * @param type
 * @param userId
 * @param offset
 * @param limit
 * @param q
 * @returns {Promise<void>}
 */
async function list({ type, userId, offset, limit, q, provinceId, cityId, areaId, time }) {
  const where = {
    status: 1,
    cStatus: 1, // 销课过后的不出现在列表
    isSend: 0
  };
  type = Number(type);
  if (type) {
    where.type = type;
    if (type === 2) {
      where.isBuy = 0;
    }
    if (type !== 2) {
      where.signEndTime = {
        $gte: new Date()
      }
    }
  }
  if (q) {
    where.courseName = {
      $like: `%${q}%`
    }
  }
  if (provinceId) {
    where.provinceId = provinceId
  }
  if (cityId) {
    where.cityId = cityId
  }
  if (areaId) {
    where.areaId = areaId
  }
  if (time) {
    const startDay = moment(time).startOf('day');
    const endDay = moment(time).endOf('day');
    where.startTime = {
      $gte: new Date(startDay),
      $lte: new Date(endDay)
    }
  }
  const [list, count] = await Promise.all([
    JJCourse.findAll({
      where,
      raw: true,
      offset,
      limit,
      attributes: ['type', 'id', 'courseName', 'classNum', 'startTime', 'classTime',
        'totalMoney', 'peopleNum', 'label', 'storeName', 'description', 'JUserId'],
      order: [['id', 'desc']]
    }),
    JJCourse.count({
      where
    })
  ]);
  const ids = [];
  list.forEach((item) => {
    ids.push(item.id);
  });
  if (list.length === 0) {
    return {
      list: [],
      count: 0
    }
  }
  const { imageMap } = await courseCommonService.queryCourseOtherInfo(ids, { isImage: 1 });
  list.map((item) => {
    item.startTime = tools.formatHHMM(item.startTime);
    item.images = imageMap[item.id] && imageMap[item.id] || [];
    return item;
  });
  return {
    list,
    count
  }
}

async function getDetail({ courseId, userId }) {
  userId = Number(userId);
  // fixme 这个地方要不要加了status等于1的条件，下架和取消了不管
  const detail = await JJCourse.find({
    where: {
      id: courseId,
      status: 1
    },
    attributes: ['type', 'id', 'courseName', 'classNum', 'startTime', 'classTime', 'provinceId', 'cityId', 'areaId', 'lat',
      'lon', 'totalMoney', 'peopleNum', 'label', 'storeName', 'description', 'JUserId', 'cStatus', 'isSend',
      'signEndTime', 'maxNum'],
    raw: true
  });
  if (!detail) {
    throw BaseResp.error(errorCode.COURSE.NOT_FOUND)
  }
  let details = [];
  const obj = {
    belongsToJiao: 0, // 是否是教练自己
    hasBuy: 0, // 用户是否买过
    canBuy: 0, // 是否可以购买即预约
    canCancel: 0, // 是否可以取消
    sellteStatus: 0,  //
    isBuy: 0
  };
  const { id, JUserId, type, cStatus, signEndTime, startTime, isSend } = detail;
  if (type === 2) {
    const courseInfo = await JJUserCourse.findOne({
      where: {
        courseId,
        state: 1 // 这种状态值
      },
      attributes: ['id'],
      raw: true
    });
    if (courseInfo) {
      obj.isBuy = 1;
    }
  }
  if (userId) {
    // 如果是教练
    if (JUserId === userId) {
      obj.belongsToJiao = 1;
      if (cStatus === 1) { // cStatus只有1和10两种状态（1代表待销课，10代表已销课）
        obj.canCancel = 1; // 还未完全销课的可以取消课程
        if (new Date(startTime) < Date.now) {
          obj.sellteStatus = 1;
        }
      } else {
        obj.sellteStatus = 10;
        obj.canCancel = 0; // 已经销课过的不能再取消了
      }
      const camps = await JJCourseCamp.findAll({
        where: {
          courseId
        },
        attributes: ['className', 'startTime', 'storeName', 'id', 'courseId', 'cStatus'],
        raw: true
      });
      camps.map((item) => {
        item.startTime = tools.formatHHMM(item.startTime);
        item.sellteStatus = 0; // 查询预约
        // 当处于待销课，课程开始后，可以销课
        if (item.cStatus === 1 && new Date(item.startTime) < Date.now) {
          item.sellteStatus = 1; // 待销课（和开始时间还是挂钩）
        }
        if (item.cStatus === 10) {
          item.sellteStatus = 10; // 已销课
        }
        delete item.cStatus;
        return item;
      });
      details = camps;
    } else {
      // 不是教练
      const userCourse = await JJUserCourse.findOne({
        where: {
          courseId,
          jUserId: userId,
          type: 1,
          state: 1 // 加一个这种状态(可能用户存在课程购买后退款)退款的这个就查不出来了(不是用的课有很多第一会===)
        },
        attributes: ['id', 'status', 'state'],
        raw: true
      });
      // 如果课程已经买过
      if (userCourse) {
        obj.hasBuy = 1;
        const { status } = userCourse; // 根据用户课程的状态来区分
        // 还没完全销课 则能够取消
        if (status >= 0 && status < 10) { // 这里考虑下 是用课程的状态（）
          obj.canCancel = 1;
        }
        const camps = await JJCourseCamp.findAll({
          where: {
            courseId
          },
          attributes: ['className', 'startTime', 'storeName', 'id', 'courseId', 'cStatus'],
          raw: true
        });
        let campMap = {};
        if (camps.length > 0) {
          // 查询哪些课时用户有预约
          const campIds = [];
          camps.forEach((item) => {
            campIds.push(item.id)
          });
          const classList = await JJUserCourse.findAll({
            where: {
              campId: campIds,
              jUserId: userId,
              state: 1
            },
            attributes: ['courseId', 'campId', 'status'],
            raw: true
          });
          campMap = _.keyBy(classList, 'campId')
        }
        camps.map((item) => {
          item.startTime = tools.formatHHMM(item.startTime);
          item.sellteStatus = 0;
          item.canSub = 0;
          item.hasSub = 0;
          // 当处于待销课，课程开始后，可以销课
          if (item.cStatus === 1 && new Date(item.startTime) < Date.now) {
            item.sellteStatus = 1;
          }
          if (item.cStatus === 10) {
            item.sellteStatus = 10; // 已销课
          }
          // 如果存在
          if (campMap[item.id]) {
            if (item.sellteStatus === 0 && campMap[item.id].status === 1) {
              item.canSub = 1;
            }
            if (campMap[item.id].status > 1) {
              item.hasSub = 1;
            }
          }
          return item;
        });
        details = camps
      } else {
        if (new Date(signEndTime).getTime() > Date.now() && obj.isBuy === 0) {
          obj.canBuy = 1;
        } else {
          if (type === 2 && obj.isBuy === 0) {
            obj.canBuy = 1;
          } else {
            obj.canBuy = 0;
          }
        }
      }
    }
  } else {
    if (new Date(signEndTime).getTime() > Date.now()) {
      obj.canBuy = 1;
    } else {
      obj.canBuy = 0;
    }
  }
  const [images, userInfo, count] = await Promise.all([
    JJCourseImages.findAll({
      where: {
        courseId: id
      },
      attributes: ['courseId', 'imagePath'],
      raw: true
    }),
    JJUser.find({
      where: {
        id: JUserId
      },
      attributes: ['name', 'avatar', 'id'],
      raw: true
    }),
    JJUserCourse.count({
      where: {
        courseId,
        type: 1,
        state: 1
      },
      raw: true
    })
  ]);
  let areaWhere = {};
  if (detail.provinceId) {
    areaWhere = {
      provinceId: detail.provinceId,
      type: 1
    }
  }
  if (detail.cityId) {
    areaWhere = {
      cityId: detail.cityId,
      type: 2
    }
  }
  if (detail.areaId) {
    areaWhere = {
      areaId: detail.areaId,
      type: 3
    }
  }
  let addressInfo = {};
  if (areaWhere.hasOwnProperty('type')) {
    const city = await City.findOne({
      where: areaWhere,
      raw: true,
      attributes: ['provinceId', 'cityId', 'areaId', 'provinceName', 'cityName', 'areaName', 'type'],
    });
    addressInfo = city
  }
  detail.images = images || [];
  detail.userInfo = {
    name: userInfo && userInfo.name || '',
    avatar: userInfo && userInfo.avatar || ''
  };
  detail.startTime = tools.formatDate(detail.startTime);
  detail.signEndTime = tools.formatDate(detail.signEndTime);
  detail.details = details;
  detail.addressInfo = addressInfo;
  detail.currentCount = count;
  Object.assign(detail, obj);
  return detail;
}

async function sellte({ courseId, campId, users, images, description, userId}) {
  // 查询用户和课程的关系
  const course = await JJCourse.findOne({
    where: {
      JUserId: userId,
      id: courseId
    },
    attributes: ['id', 'cStatus', 'type', 'JUserId', 'isSend', 'signEndTime'],
    raw: true
  });
  if (!course) {
    throw BaseResp.error(errorCode.COURSE.NOT_FOUND)
  }
  const { cStatus, type, JUserId, isSend, signEndTime } = course;
  if (new Date().getTime() < new Date(signEndTime).getTime()) {
    throw BaseResp.error(errorCode.COURSE.CAN_NOT_SETTLE)
  }
  // 已经销课
  if (cStatus === 10) {
    throw BaseResp.error(errorCode.COURSE.HAS_END_COURSE)
  }
  // 团课销课 (没有课时，整个课程直接销)
  if (type === 1) {
    await courseCommonService.courseTuanSellte({ users, courseId, lUserId: JUserId, description, images })
  } else if (type === 2) { // 私教销课
    // 如果是分享出去的这种，是有课时的，不是分享的没有课时
    if (isSend) {
      if (!Number(campId)) {
        throw BaseResp.error(errorCode.COURSE.CHOOSE_END_CLASS)
      }
      await courseCommonService.courseSiSettle({ campId, courseId, images, users, description, lUserId: JUserId });
    } else {
      // 不是分享的这种的可以参考团课,
      await courseCommonService.courseTuanSellte({ users, courseId, lUserId: JUserId, description, images })
    }
  } else if (type === 3) { // 训练营
    if (!Number(campId)) {
      throw BaseResp.error(errorCode.COURSE.CHOOSE_END_CLASS)
    }
    await courseCommonService.courseSiSettle({ campId, courseId, images, users, description, lUserId: JUserId });
  }
}

async function getUserList({ courseId, userId, campId }) {
  // offset = offset && Number(offset) || 0;
  // limit = limit && Number(limit) || 20;
  const where = {
    courseId,
    state: 1
  };
  if (Number(campId)) {
    where.campId = campId;
    // where.isJiao = 0;
  }
  const userList = await JJUserCourse.findAll({
    where,
    order: [['id', 'desc']],
    attributes: ['jUserId', 'status', 'courseId', 'isJoin'],
    raw: true
  });
  const userIds = [];
  userList.forEach((item) => {
    userIds.push(item.jUserId);
  });
  let userMap = {};
  if (userIds.length > 0) {
    const userList = await JJUser.findAll({
      where: {
        id: userIds
      },
      attributes: ['name', 'avatar', 'id', 'phone'],
      raw: true
    });
    userMap = _.keyBy(userList, 'id')
  }
  userList.map((item) => {
    item.userName = userMap[item.jUserId] && userMap[item.jUserId].name || '';
    item.avatar = userMap[item.jUserId] && userMap[item.jUserId].avatar || '';
    item.phone = userMap[item.jUserId] && userMap[item.jUserId].phone || '';
    return item;
  });
  return { list: userList };
}

async function addCamp({ courseId, details, userId }) {
  // 查询此课程是不是私教私人定义 是的话 才能够进行购买
  const course = await JJCourse.findOne({
    where: {
      id: courseId,
      JUserId: userId,
      type: 2, // 私教
      isSend: 1, // 是私人分享课程
      cStatus: 1 // 没有销过课的
    },
    raw: true
  });
  if (!course) { // 课程不存在
    throw BaseResp.error(errorCode.COURSE.NOT_FOUND)
  }
  const { JUserId } = course;
  const array = [];
  details.forEach((item) => {
    array.push({
      className: item.className,
      storeName: item.storeName,
      startTime: new Date(item.classStartTime),
      courseId,
      cStatus: 1,
      JUserId: userId
    })
  });
  const userCourseInfo = await JJUserCourse.findOne({
    where: {
      state: 1,
      courseId,
      type: 1
    },
    attributes: ['jUserId'],
    raw: true
  });
  if (!userCourseInfo) {
    throw BaseResp.error(errorCode.COURSE.NOT_MAKE_CAMP)
  }
  const { jUserId: userCourseId } = userCourseInfo;
  // todo 考虑下如何让用户二次确认
  const datas = await JJCourseCamp.bulkCreate(array);
  const userCourseArray = [];
  datas.forEach((item) => {
    userCourseArray.push({
      jUserId: userCourseId,
      status: 1,
      courseId,
      campId: item.id,
      type: 2,
      lUserId: JUserId
    })
  });
  await JJUserCourse.bulkCreate(userCourseArray)
}

async function getCourseUserList({ offset, limit }) {
  const where = {
    identity: 1, // 获取教练列表
  };
  const [list, count] = await Promise.all([
    JJUser.findAll({
      where,
      attributes: ['id', 'avatar', 'name', 'description'],
      raw: true,
      offset,
      limit,
      order: [['id', 'desc']]
    }),
    JJUser.count({ where })
  ]);
  return {
    list, count
  }
}

async function modify({ courseId, userId, bigCourse, selfCourse, camp }) {
  const count = await JJUserCourse.count({
    where: {
      courseId,
      type: 1,
      state: 1
    },
    raw: true
  });
  // 超过最大人数报名限制
  if (count >= 1) {
    throw BaseResp.error(errorCode.COURSE.HAS_BUY_CAN_NOT_MODIFY)
  }
  const course = await JJCourse.find({
    where: {
      id: courseId,
      JUserId: userId,
      status: 1
    }
  });
  if (!course) {
    throw BaseResp.error(errorCode.COURSE.NOT_FOUND)
  }
  if (course.status !== 1) {
    throw BaseResp.error(errorCode.COURSE.CAN_NOT_MODIFY)
  }
  let images = [];
  if (course.type === 1) {
    images = bigCourse.images;
    Object.assign(course, bigCourse, bigCourse.addressInfo || {})
  } else if (course.type === 2) {
    images = selfCourse.images;
    delete selfCourse.signEndTime;
    Object.assign(course, selfCourse, selfCourse.addressInfo || {})
  } else if (course.type === 3) {
    images = camp.images;
    Object.assign(course, camp, camp.addressInfo || {})
  }
  if (images.length > 0) {
    const imageArr = [];
    images.forEach((item) => {
      imageArr.push({
        JUserId: userId,
        courseId: courseId,
        imagePath: item
      });
    });
    // 先删除
    await JJCourseImages.destroy({
      where: {
        courseId
      }
    });
    // 添加
    await JJCourseImages.bulkCreate(imageArr)
  }
  await course.save();
}

async function modifyCamp({ userId, campId, className, storeName, classStartTime }) {
  const courseCamp = await JJCourseCamp.findOne({
    where: {
      id: campId,
      JUserId: userId,
      cStatus: 1 // 还未销课的，因为只有1和10的状态，所以只有1的状态能修改
    },
    attributes: ['id', 'classStartTime', 'courseId'],
    raw: true
  });
  if (!courseCamp) {
    throw BaseResp.error(errorCode.COURSE.CAN_NOT_MODIFY)
  }
  const { classStartTime: courseStartTime, courseId } = courseCamp;
  if (new Date(courseStartTime).getTime() > Date.now()) {
    throw BaseResp.error(errorCode.COURSE.CAMP_IS_START)
  }
  if (new Date(classStartTime).getTime() < Date.now()) {
    throw BaseResp.error(errorCode.COURSE.START_TIME_LESS)
  }
  const course = await JJCourse.findOne({
    where: {
      id: courseId
    },
    attributes: ['id', 'status', 'startTime'],
    raw: true
  });
  if (!course) {
    throw BaseResp.error(errorCode.COURSE.NOT_FOUND)
  }
  if (course.status !== 1) {
    throw BaseResp.error(errorCode.COURSE.CAN_NOT_MODIFY)
  }
  const update = {};
  if (className) {
    update.className = className
  }
  if (storeName) {
    update.storeName = storeName
  }
  if (classStartTime) {
    update.startTime = classStartTime
  }
  await JJCourseCamp.update(update, {
    where: {
      id: campId
    }
  });
  if (new Date(course.startTime).getTime() > new Date(classStartTime).getTime()) {
    await JJCourse.update({
      startTime: classStartTime
    }, {
      where: {
        id: courseId
      }
    })
  }
}

async function remove({ courseId, userId }) {
  const course = await JJCourse.find({
    where: {
      id: courseId,
      JUserId: userId,
      status: 1
    },
    raw: true
  });
  if (!course) {
    throw BaseResp.error(errorCode.COURSE.CAN_NOT_REMOVE)
  }
  await JJCourse.update({
    status: -1
  }, {
    where: {
      id: course.id
    }
  })
}

async function report({ courseId, kind, reason, userId }) {
  const userCourse = await JJUserCourse.findOne({
    where: {
      courseId,
      jUserId: userId
    },
    raw: true,
    attributes: ['id']
  });
  if (!userCourse) {
    throw BaseResp.error(errorCode.COURSE.NOT_BUY)
  }
  kind = kind.join(',');
  const ticketInfo = await JJCourseTicket.findOne({
    where: {
      jUserId: userId,
      courseId
    },
    attributes: ['id'],
    raw: true
  });
  if (ticketInfo) {
    throw BaseResp.error(errorCode.COURSE.COURSE_HAS_TICKET)
  }
  await JJCourseTicket.create({
    status: 0,
    courseId,
    jUserId: userId,
    refuseReason: '',
    reportKind: kind,
    reportReason: reason
  })
}

async function reportList({ offset, limit, userId }) {
  const where = {
    jUserId: userId
  };
  const [list ,count] = await Promise.all([
    JJCourseTicket.findAll({
      where,
      offset,
      limit,
      attributes: ['courseId', 'jUserId', 'refuseReason', 'reportReason', 'status', 'createTime', 'reportKind', 'id'],
      raw: true
    }),
    JJCourseTicket.count({
      where
    })
  ]);
  const courseIds = [];
  const userIds = [];
  list.forEach((item) => {
    courseIds.push(item.courseId);
    userIds.push(item.jUserId)
  });
  const [users, courses] = await Promise.all([
    JJUser.findAll({
      where: {
        id: userIds
      },
      raw: true,
      attributes: ['name', 'id'],
    }),
    JJCourse.findAll({
      where: {
        id: courseIds
      },
      attributes: ['id', 'courseName'],
      raw: true
    })
  ]);
  const userMap = _.keyBy(users, 'id');
  const courseMap = _.keyBy(courses, 'id');
  list.map((item) => {
    item.userName = userMap[item.jUserId] && userMap[item.jUserId].name || '';
    item.userId = userMap[item.jUserId] && userMap[item.jUserId].id || 0;
    item.courseName = courseMap[item.courseId] && courseMap[item.courseId].courseName || '';
    item.courseId = courseMap[item.courseId] && courseMap[item.courseId].id || 0;
    item.createTime = tools.formatDate(item.createTime);
    if (item.reportKind) {
      item.reportKind = item.reportKind.split(',');
      item.reportKind = item.reportKind.map((item1) => {
        if (item1 === '1') {
          return '态度恶劣'
        } else if (item1 === '2') {
          return '未按时上课'
        } else if (item1 === '3') {
          return '随意消课'
        } else {
          return '其他'
        }
      })
    } else {
      item.reportKind = [];
    }
    return item;
  });
  return {
    list,
    count
  }
}

async function cancel({ courseId, userId }) {
  const course = await JJCourse.findOne({
    where: {
      id: courseId,
      JUserId: userId
    },
    attributes: ['status', 'type', 'isSend', 'startTime'],
    raw: true
  });
  if (!course) {
    throw BaseResp.error(errorCode.COURSE.NOT_FOUND)
  }
  const { status, startTime, isSend, type } = course;
  if (status !== 1) {
    throw BaseResp.error(errorCode.COURSE.COURSE_CAN_NOT_CANCEL)
  }
  // 这种开始时间比当前时间要小 说明课程还未开始，直接全退
  if (new Date(startTime).getTime() < Date.now()) {
    // fixme 这种金额全退

   }
}

async function active({ courseId, userId }) {
  // 只有私教才能进行黄建课程后确认
}


module.exports = {
  // 添加
  add,
  // 修改
  modify,
  // 删除
  remove,
  // 列表
  list,
  // 取消
  cancel,
  // 详情
  getDetail,
  // 销课
  sellte,
  // 用户列表
  getUserList,
  // 增加课时
  addCamp,
  // 获取教练列表
  getCourseUserList,
  // 修改课时
  modifyCamp,
  // 举报
  report,
  // 举报列表
  reportList,
  // 举报郑正确形似 证据金杯]]]
  active
};
